
package org.apache.ocean.udp;

import java.io.*;
import java.util.concurrent.locks.*;
import org.apache.ocean.*;
import java.nio.charset.*;
import java.util.logging.*;
import java.util.*;
import org.apache.commons.lang.*;
import java.net.*;
import org.jdom.Element;

/**
 *
 * @author jasonr
 */
public class UDPMessage {
  public static Logger log = Logger.getLogger(UDPMessage.class.getName());
  public String channelName;
  public String fromID;
  public URL fromURL;
  public String fromRole;
  public String action;
  public Element content;
  
  public UDPMessage() {}
  
  public UDPMessage(String xml) throws Exception {
    init(XMLUtil.parseElement(xml));
  }
  
  public UDPMessage(Element element) {
    init(element);
  }
  
  public String toXML() throws Exception {
    return XMLUtil.outputElementMinimal(toElement());
  }
  
  private void init(Element element) {
    fromID = element.getChildText("fromID");
    fromURL = XMLUtil.getChildURL("fromURL", element);
    fromRole = element.getChildText("fromRole");
    action = element.getChildText("action");
    channelName = element.getChildText("channelName");
    Element contentElement = element.getChild("content");
    Element contentChildElement = (Element)ClusterUtil.getFirst(contentElement.getChildren());
    content = ClusterUtil.removeParentElement(contentChildElement);
  }
  
  public DatagramPacket toPacket(InetSocketAddress address) throws Exception {
    Element element = toElement();
    String xml = XMLUtil.outputElementMinimal(element);
    ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
    DataOutputStream dataOutput = new DataOutputStream(byteOutput);
    //log.log(Level.INFO, xml);
    dataOutput.writeUTF(xml);
    byte[] bytes = byteOutput.toByteArray();
    DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address);
    return packet;
  }
  
  public static UDPMessage toMessage(DatagramPacket packet) throws Exception {
    byte[] bytes = packet.getData();
    ByteArrayInputStream byteInput = new ByteArrayInputStream(bytes);
    DataInputStream dataInput = new DataInputStream(byteInput);
    String xml = dataInput.readUTF();
    //log.log(Level.INFO, xml);
    Element element = XMLUtil.parseElement(xml);
    return new UDPMessage(element);
  }
  
  public String toString() {
    Element element = toElement();
    return XMLUtil.outputElementOmitDeclaration(element);
  }
  
  public org.jdom.Element toElement() {
    Element element = new Element("udpMessage");
    XMLUtil.createTextElement("fromID", fromID, element);
    XMLUtil.createTextElement("fromURL", fromURL, element);
    XMLUtil.createTextElement("fromRole", fromRole, element);
    XMLUtil.createTextElement("action", action, element);
    
    if (content != null) {
      Element contentElement = new Element("content");
      content = ClusterUtil.removeParentElement(content);
      contentElement.addContent(content);
      element.addContent(contentElement);
    }
    return element;
  }
}
